package com.irdstudio.tdp.executor.core.dao;

import com.irdstudio.tdp.executor.core.dao.domain.MigrateTableField;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 迁移表字段类-[表名: 迁移表字段(migrate_table_field)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2020-11-13
 */
public class MigrateTableFieldDao {

	/* 连接对象 */
	Connection conn = null;	

	public MigrateTableFieldDao(Connection conn){
		this.conn = conn;
	}


    /**
	* 新增一行
	* @return int
	*/
    public int insertMigrateTableField(MigrateTableField migrateTableField) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("insert into migrate_table_field ( "
                + "table_id,field_code,field_name,field_type,field_option,field_is_pk"
                + ") values ("+ "?,?,?,?,?,?" +")"
                );
            ps.setObject(1, migrateTableField.getTableId());
            ps.setObject(2, migrateTableField.getFieldCode());
            ps.setObject(3, migrateTableField.getFieldName());
            ps.setObject(4, migrateTableField.getFieldType());
            ps.setObject(5, migrateTableField.getFieldOption());
            ps.setObject(6, migrateTableField.getFieldIsPk());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("insert MigrateTableField is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键删除一行记录
	* @return int
	*/
    public int deleteByPk(MigrateTableField migrateTableField) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("delete from migrate_table_field where 1=1"
                + " AND table_id = ? "
                + " AND field_code = ? "
                );
            ps.setObject(1, migrateTableField.getTableId());
            ps.setObject(2, migrateTableField.getFieldCode());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("delete MigrateTableField is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键更新一行记录
	* @return int
	*/
    public int updateByPk(MigrateTableField migrateTableField) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("update migrate_table_field set "
                + " table_id = ? ,"
                + " field_code = ? ,"
                + " field_name = ? ,"
                + " field_type = ? ,"
                + " field_option = ? ,"
                + " field_is_pk = ? "
                + " where 1=1"
                + " AND table_id = ? "
                + " AND field_code = ? "
                );
            ps.setObject(1, migrateTableField.getTableId());
            ps.setObject(2, migrateTableField.getFieldCode());
            ps.setObject(3, migrateTableField.getFieldName());
            ps.setObject(4, migrateTableField.getFieldType());
            ps.setObject(5, migrateTableField.getFieldOption());
            ps.setObject(6, migrateTableField.getFieldIsPk());
            ps.setObject(7, migrateTableField.getTableId());
            ps.setObject(7, migrateTableField.getFieldCode());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("update MigrateTableField is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }

	}

    /**
	* 根据主键查询一行记录
	* @return int
	*/
    public MigrateTableField queryByPk(MigrateTableField migrateTableField) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        MigrateTableField result = null;
        try {
            ps = conn.prepareStatement("select table_id,field_code,field_name,field_type,field_option,field_is_pkfrom migrate_table_field where 1=1 "
                + " AND table_id = ? "
                + " AND field_code = ? "
                );
            ps.setObject(1, migrateTableField.getTableId());
            ps.setObject(2, migrateTableField.getFieldCode());
            rs = ps.executeQuery();
            if (rs.next()) {
				result = new MigrateTableField();
				result.setTableId(rs.getString("table_id"));
				result.setFieldCode(rs.getString("field_code"));
				result.setFieldName(rs.getString("field_name"));
				result.setFieldType(rs.getString("field_type"));
				result.setFieldOption(rs.getString("field_option"));
				result.setFieldIsPk(rs.getString("field_is_pk"));
			}
        } catch (SQLException e) {
            throw new SQLException("update MigrateTableField is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	* 根据 migrateTableField 参数列表查询 不分页
	* @return int
	*/
    public List<MigrateTableField> queryAll(MigrateTableField migrateTableField) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<MigrateTableField> result = new ArrayList<>();
        try {
            String sql = "select table_id,field_code,field_name,field_type,field_option,field_is_pkfrom migrate_table_field where 1=1 ";
                if (migrateTableField.getTableId() != null) {
                    sql = sql + " AND table_id =  '" + migrateTableField.getTableId() + "'";
                }
                if (migrateTableField.getFieldCode() != null) {
                    sql = sql + " AND field_code =  '" + migrateTableField.getFieldCode() + "'";
                }
                if (migrateTableField.getFieldName() != null) {
                    sql = sql + " AND field_name =  '" + migrateTableField.getFieldName() + "'";
                }
                if (migrateTableField.getFieldType() != null) {
                    sql = sql + " AND field_type =  '" + migrateTableField.getFieldType() + "'";
                }
                if (migrateTableField.getFieldOption() != null) {
                    sql = sql + " AND field_option =  '" + migrateTableField.getFieldOption() + "'";
                }
                if (migrateTableField.getFieldIsPk() != null) {
                    sql = sql + " AND field_is_pk =  '" + migrateTableField.getFieldIsPk() + "'";
                }
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
				MigrateTableField obj = new MigrateTableField();
				obj.setTableId(rs.getString("table_id"));
				obj.setFieldCode(rs.getString("field_code"));
				obj.setFieldName(rs.getString("field_name"));
				obj.setFieldType(rs.getString("field_type"));
				obj.setFieldOption(rs.getString("field_option"));
				obj.setFieldIsPk(rs.getString("field_is_pk"));
                result.add(obj);
			}
        } catch (SQLException e) {
            throw new SQLException("update MigrateTableField is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
    protected void close(ResultSet theRs, Statement theStmt, PreparedStatement thePs) {
        try {
            if (theRs != null)
                theRs.close();
            if (theStmt != null)
                theStmt.close();
            if (thePs != null)
                thePs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
